package com.xflib.tool.builder;

import cn.hutool.core.util.StrUtil;
import org.slf4j.LoggerFactory;

public class NpmBuilder extends AbstractBuilder {
    public NpmBuilder(BuilderFactory builderFactory) {
        super(builderFactory);
        super.setLogger(LoggerFactory.getLogger(this.getClass()));
    }

    @Override
    public String getId() {
        return "npm";
    }

    @Override
    public String getDescribe() {
        return "NPM builder.";
    }

    @Override
    protected String getCommandCheckout(BuilderProjectProperties properties, String branch) {
        return String.format("git checkout %s",branch); //" & git.exe reset --hard & git pull")
    }

    @Override
    protected String getCommandClone(BuilderProjectProperties properties) {
        return String.format("git clone %s",properties.getSourceUrl());
    }

    @Override
    protected String getCommandPull(BuilderProjectProperties properties) {
        return "git pull & powershell -c \"((Git status) -match 'On branch' -split '\\s')[2]\"";
    }

    @Override
    protected String getCommandBuild(BuilderProjectProperties properties) {
        String profile=properties.getProfile();
        if(null==profile || profile.trim().isEmpty()){
            profile = "";
        }else{
            profile=":".concat(profile);
        }
        return String.format("npm i & npm run build%s", profile);
    }

    @Override
    protected String getCommandDeploy(BuilderProjectProperties project, String zipFileName){
        String remoteTarget = project.getRemotePath().trim();
        if(remoteTarget.endsWith("/")) {
            remoteTarget = remoteTarget.substring(0, remoteTarget.length()-1);
        }
        String projectName = project.getProjectName();
        if(StrUtil.isBlank(remoteTarget) || remoteTarget.equals("/")){
            return "echo work path can not on `pwd`.;echo '>>>>>>' "+projectName+" install failed.;";
        }

        return "source /etc/profile;"
             + "source ~/.bash_profile;"
             + "if [ ! -d \"/tmp\" ];then mkdir /tmp;fi;cd /tmp;"  // 为了避免在根目录操作出错，设置工作目录为/tmp
             + "ZIP_FILE=/tmp/"+ zipFileName +";"
             + "PRJ_NAME=" + projectName + ";"
             + "PRJ_PATH=" + remoteTarget + ";"
             + "BAK_PATH=${PRJ_PATH}/../../backup;"
             + "if [ ! -d \"${BAK_PATH}\" ];then mkdir -p ${BAK_PATH};fi;"
             + "if [ ! -d ${PRJ_PATH} ];then mkdir -p ${PRJ_PATH};touch ${PRJ_PATH}/index.html;fi;"
             + "if [ -f \"${PRJ_PATH}/index.html\" ];"
             + "then TLT=`stat --format=%y ${PRJ_PATH}/index.html`;TLT=`date -d \"${TLT}\" +%Y%m%d%H%M%S`;"
             + "else TLT=0;"
             + "fi;"
             + "if [ ! -d ${BAK_PATH} ];then mkdir ${BAK_PATH};fi;"
             + "mv ${PRJ_PATH} ${BAK_PATH}/${PRJ_NAME}.${TLT};" // 之前的目标备份到backup
             + "unzip -O UTF-8 ${ZIP_FILE} -d ${PRJ_PATH};"  // 在java程序中(环境是UTF-8)压缩的, 所以UTF-8，若是windows下压缩的，则用CP936
             + "echo ${PRJ_NAME} install success.;";

    }
}
